import { DD_SYSTEM } from '@/constants/api-pre';
import { request } from '@/service/request';

export type Predicates = {
  name: string;
  args: any;
};

export type PredicatesArgs = {
  key: string;
  value: any;
};

export type Filters = {
  title: string;
  name: string;
  args: PredicatesArgs[];
};

/** GatewayRoute */
export type GatewayRoute = Api.Common.CommonRecord<{
  routerId: string;
  name: string;
  uri: string;
  predicates: string;
  filters: string;
  retryable: number | null;
  stripPrefix: string;
  persistable: string;
  showApi: string;
}>;

/** lGatewayRoute search params */
export type GatewayRouteSearchParams = CommonType.RecordNullable<
  Pick<GatewayRoute, 'routerId' | 'name'> & Api.Common.CommonSearchParams
>;

export type GatewayRouteEditAndCreateParams = {
  id: number;
  routerId: string;
  name: string;
  uri: string;
  predicates: Predicates[];
  filters: Filters[];
  retryable: number | null;
  stripPrefix: string;
  persistable: string;
  showApi: string;
  status: number;
};

/** GatewayRoute list */
export type GatewayRouteList = Api.Common.PaginatingQueryRecord<GatewayRoute>;

/** fetch GatewayRoute list */
export function fetchGetGatewayRouteList(params?: GatewayRouteSearchParams) {
  return request<GatewayRouteList>({
    url: `${DD_SYSTEM}/gateway-route/page`,
    method: 'get',
    params
  });
}

/**
 * 新增 GatewayRoute
 *
 * @param data
 */
export function createGatewayRoute(data: any) {
  return request<Boolean>({
    url: `${DD_SYSTEM}/gateway-route`,
    method: 'post',
    data
  });
}

/**
 * 编辑 GatewayRoute
 *
 * @param id
 * @param data
 */
export function updateGatewayRoute(id: number, data?: any) {
  return request<Boolean>({
    url: `${DD_SYSTEM}/gateway-route/${id}`,
    method: 'put',
    data
  });
}

/**
 * 删除 GatewayRoute
 *
 * @param id
 */
export function deleteGatewayRoute(id: string) {
  return request<Boolean>({
    url: `${DD_SYSTEM}/gateway-route/${id}`,
    method: 'delete'
  });
}
